<template>
<div>
  <v-md-editor v-model="text" height="500px"></v-md-editor>
</div>
</template>
<script>
  export default {
  data() {
    return {
      text: '',
      ws:null
    };
  },
  mounted(){
    if(window.WebSocket){
      //实例化出来对象
      this.ws = new WebSocket("ws://localhost:8181");
    }
    this.ws.onopen=(e)=>{
        console.log('链接成功')
        this.ws.send(JSON.stringify({
          chat:this.text
        }))
      }
      this.ws.onmessage=(e)=>{
         let resData = JSON.parse(e.data);
         console.log(resData)
         this.text = resData.chat;
      }
      this.ws.onerror=()=>{
        console.log('出错了')
      }
  },
  watch:{
    text(newval,oldval){
      if(newval!==oldval){
        this.ws.send(JSON.stringify({
          chat:newval
        }))
      }
    }
  }
};

</script>
